package top.lidee.module.clientApi.twice.applet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.UserInfo;
import top.lidee.common.service.GoodsService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.vo.management.SaveOrUpdateGoodsAppletVo;
import top.lidee.repository.validator.groups.Update;
import top.lidee.utils.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 小程序商品管理
 * @date 2024/9/27 21:03
 */
@Api(tags="小程序端-店铺商品管理")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/applet/goods")
public class MtGoodsController {

    private final GoodsService goodsService;

    /**
     * 根据店铺id查询商品
     */
    @RequestMapping(value = "/getGoodsByStoreId/{storeId}", method = RequestMethod.GET)
    @ApiOperation(value = "根据店铺id查询商品")
    public ResponseResult getGoodsByStoreId(HttpServletRequest request, @PathVariable("storeId") Integer storeId) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.getGoodsByStoreId(userInfo,storeId);
        return responseResult;
    }


    /**
     * 查询商品分类列表 （没用）
     */
    @RequestMapping(value = "/getGoodsCates", method = RequestMethod.GET)
    @ApiOperation(value = "查询商品分类列表")
    @Deprecated
    public ResponseResult getGoodsCates(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.getGoodsCates(userInfo);
        return responseResult;
    }

    /**
     * 店铺新增商品
     */
    @RequestMapping(value = "/saveGoodsApplet", method = RequestMethod.POST)
    @ApiOperation(value = "店铺新增商品")
    public ResponseResult saveGoodsApplet(HttpServletRequest request, @RequestBody @Valid SaveOrUpdateGoodsAppletVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.saveGoodsApplet(userInfo,vo);
        return responseResult;
    }

    /**
     * 根据商品id回显商品数据
     */
    @RequestMapping(value = "/goodsAppletById/{goodsId}", method = RequestMethod.GET)
    @ApiOperation(value = "根据商品id回显商品数据")
    public ResponseResult goodsAppletById(HttpServletRequest request, @PathVariable("goodsId") Integer goodsId) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.goodsAppletById(userInfo,goodsId);
        return responseResult;
    }

    /**
     * 修改商品
     */
    @RequestMapping(value = "/updateGoodsApplet", method = RequestMethod.PUT)
    @ApiOperation(value = "修改商品")
    public ResponseResult updateGoodsApplet(HttpServletRequest request, @RequestBody @Validated(Update.class) SaveOrUpdateGoodsAppletVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.updateGoodsApplet(userInfo,vo);
        return responseResult;
    }

    /**
     * 商品修改后查询需要字段
     */
    @RequestMapping(value = "/afterTheProductIsModified/{storeId}", method = RequestMethod.GET)
    @ApiOperation(value = "商品修改后查询需要字段")
    public ResponseResult afterTheProductIsModified(HttpServletRequest request,
                                                    @PathVariable("storeId") Integer storeId) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.afterTheProductIsModified(userInfo,storeId);
        return responseResult;
    }

    /**
     * 根据id修改上下架状态
     */
    @RequestMapping(value = "/updateStatusById/{goodsId}", method = RequestMethod.PUT)
    @ApiOperation(value = "根据id修改上下架状态")
    public ResponseResult updateStatusById(HttpServletRequest request, @PathVariable("goodsId") Integer goodsId) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.updateStatusById(userInfo,goodsId);
        return responseResult;
    }

    /**
     * 单个或批量删除商品
     */
    @RequestMapping(value = "/deleteByGoodsIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "单个或批量删除商品")
    public ResponseResult deleteByGoodsIds(HttpServletRequest request, @RequestBody Integer[] goodsIds) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = goodsService.deleteByGoodsIds(userInfo,goodsIds);
        return responseResult;
    }

}
